package cn.life.common.support.datasource.config;

import cn.life.common.support.datasource.config.properties.DruidDataSourcePoolProperties;
import cn.life.common.support.datasource.config.properties.MysqlDataSourceProperties;
import cn.life.common.support.datasource.config.properties.OracleDataSourceProperties;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.annotation.Order;

import javax.sql.DataSource;

/**
 * @Description:
 * @Author 李佳明
 * @Date 2018/6/18 0018.19:13.
 */
@Order(102)
@Configuration
public class DataSourceConfiguration{

    @Autowired
    private DruidDataSourcePoolProperties poolProperties;

    @Autowired
    private MysqlDataSourceProperties mysqlProperties;

    @Autowired
    private OracleDataSourceProperties oracleProperties;

    @Primary
    @Bean(name = "mySqlDataSource")
    public DataSource mysqlDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(mysqlProperties.getDriverClassName());
        dataSource.setUrl(mysqlProperties.getUrl());
        dataSource.setUsername(mysqlProperties.getUsername());
        dataSource.setPassword(mysqlProperties.getPassword());
        initDataSourcePoolConfiguration(dataSource);
        return dataSource;
    }

    @Bean(name = "oracleDataSource")
    public DataSource oracleDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(oracleProperties.getDriverClassName());
        dataSource.setUrl(oracleProperties.getUrl());
        dataSource.setUsername(oracleProperties.getUsername());
        dataSource.setPassword(oracleProperties.getPassword());
        initDataSourcePoolConfiguration(dataSource);
        return dataSource;
    }


    private void initDataSourcePoolConfiguration(DruidDataSource dataSource) {
        Integer initialSize = poolProperties.getInitialSize();
        if (initialSize != null) {
            dataSource.setInitialSize(initialSize);
        }
        Integer maxActive = poolProperties.getMaxActive();
        if (maxActive != null) {
            dataSource.setMaxActive(maxActive);
        }
        Long maxWait = poolProperties.getMaxWait();
        if (maxWait != null) {
            dataSource.setMaxWait(maxWait);
        }
        Integer minIdle = poolProperties.getMinIdle();
        if (minIdle != null) {
            dataSource.setMinIdle(minIdle);
        }
        Long timeBetweenEvictionRunsMillis = poolProperties.getTimeBetweenEvictionRunsMillis();
        if (timeBetweenEvictionRunsMillis != null) {
            dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        }
        Long minEvictableIdleTimeMillis = poolProperties.getMinEvictableIdleTimeMillis();
        if (minEvictableIdleTimeMillis != null) {
            dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        }
        Boolean removeAbandoned = poolProperties.getRemoveAbandoned();
        if (removeAbandoned != null) {
            dataSource.setRemoveAbandoned(removeAbandoned);
        }
        Integer removeAbandonedTimeout = poolProperties.getRemoveAbandonedTimeoutMillis();
        if (removeAbandonedTimeout != null) {
            dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout);
        }
        Boolean logAbandoned = poolProperties.getLogAbandoned();
        if (logAbandoned != null) {
            dataSource.setLogAbandoned(logAbandoned);
        }
        Boolean poolPreparedStatements = poolProperties.getPoolPreparedStatements();
        if (poolPreparedStatements != null) {
            dataSource.setPoolPreparedStatements(poolPreparedStatements);
        }
        Integer maxPoolPreparedStatementPerConnectionSize = poolProperties.getMaxPoolPreparedStatementPerConnectionSize();
        if (maxPoolPreparedStatementPerConnectionSize != null) {
            dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
        }
        Boolean testOnBorrow = poolProperties.getTestOnBorrow();
        if (testOnBorrow != null) {
            dataSource.setTestOnBorrow(testOnBorrow);
        }
        Boolean testOnReturn = poolProperties.getTestOnReturn();
        if (testOnReturn != null) {
            dataSource.setTestOnReturn(testOnReturn);
        }
        Boolean testWhileIdle = poolProperties.getTestWhileIdle();
        if (testWhileIdle != null) {
            dataSource.setTestWhileIdle(testWhileIdle);
        }
        String validationQuery = poolProperties.getValidationQuery();
        if (validationQuery != null) {
            dataSource.setValidationQuery(validationQuery);
        }
    }
}
